<?php


	namespace app\index\controller;


	use app\admin\model\goods\Goods;
	use think\Controller;
	use think\Db;

	class Demo extends Controller
	{


		public function index () {

			$title = 'element-index';
			if ($this->request->isAjax()) {

				if ($this->request->isGet()) {
					$list = Goods::all();
					$this->returnSuccess($list);
				} else {
					$data = $this->request->post();
					$this->returnSuccess($data);
				}

			}
			$this->assign(['title' => $title]);

			return $this->view->fetch();
		}


		public function getRegion () {
			if ($this->request->isAjax()) {

				if ($this->request->isGet()) {
					$region = [
						['id' => 1, 'title' => '南京'],
						['id' => 2, 'title' => '合肥'],
						['id' => 13, 'title' => '武汉'],
					];
					$this->returnSuccess($region);
				} else {

				}

			}
		}

		public function getDefault () {
			if ($this->request->isAjax()) {

				if ($this->request->isGet()) {
					$data = [
						'name'   => 'lisi',
						'region' => 13,
						'desc'   => 'this is desc '
					];
					$this->returnSuccess($data);
				} else {

				}

			}
		}

		public function getMemberList () {
			if ($this->request->isAjax()) {
				if ($this->request->isGet()) {
					$page = max(1, intval($this->request->get('page')));
					$pagesize = max(4, $this->request->get('pagesize'));


					$where = [];
					$where['delete_time'] = ['=', 0];
					if ($this->request->get('keyword')) {
						$keyword = trim($this->request->get('keyword'));
						$where['mobile'] = ['LIKE', "%$keyword%"];
					}

					$count = Db::name('chat_user')->field('id,openid,nickname,country,mobile,province,unionid')->where($where)->order('id desc')->count();
					$count_page = ceil($count / $pagesize);
					$list = Db::name('chat_user')->field('id,openid,nickname,country,mobile,province,unionid')->where($where)->order('id desc')->limit(($page - 1) * $pagesize, $pagesize)->select();
					$this->returnSuccess(['list' => $list, 'count_page' => $count_page]);
				}
			}
		}


		public function edit () {

			if ($this->request->isAjax()) {
				if ($this->request->isGet()) {
					$id = intval($this->request->get('id'));
					$item = Db::name('chat_user')->field('id,openid,nickname,country,mobile,province,unionid,ipss')->where('id', $id)->find();
					//兼容动态添加节点的写法,如果没有值则要为循环留出样式
					$item['ipss'] = $item['ipss'] ? unserialize($item['ipss']) : [['username'=>'','length'=>'']];

					$this->returnSuccess($item);
				} else {
					$form = $this->request->post();
					if (isset($form['id'])) {
						$id = $form['id'];
						$form['ipss']  =  serialize($form['ipss']);
						unset($form['id']);
						$row = Db::name('chat_user')->where('id', $id)->update($form);

						if ($row) {
							$this->returnError('修改成功', 1);
						}
						$this->returnError('修改失败');
					} else {
						$form = $this->request->post();
						$form['ipss'] = serialize($form['ipss']);
						$id = Db::name('chat_user')->insertGetId($form);
						if ($id) {
							$this->returnError('新增成功', 1);
						}
						$this->returnError('新增失败');
					}
				}
			}

			return $this->view->fetch();
		}





		public function deleted () {
			if ($this->request->isAjax()) {
				$id = intval($this->request->post('id'));
				$row = Db::name('chat_user')->where('id', $id)->update(['delete_time' => time()]);
				if ($row) {
					$this->returnSuccess('删除成功', 1);
				}
				$this->returnError('删除失败');
			}
		}


		#测试增加节点
		public function tree () {


			return $this->view->fetch();

		}


	}